
package hk.ces.oxfam.patch.result;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author ReutyCheng
 */
public class BaseDataResult implements DataResult
{
  private final List<Object[]> resultsList;
  private final int columnCount;

  public BaseDataResult(List<Object[]> resultsList)
  {
    this.resultsList = resultsList != null ? resultsList : new ArrayList<Object[]>();
    this.columnCount = resultsList.isEmpty() ? 0 : resultsList.iterator().next().length;
  }

  public List<Object[]> getResultsList()
  {
    return resultsList;
  }

  public Object[] getResultsValue(int resultsIndex)
  {
    if (resultsIndex < 0 || resultsIndex >= getResultsCount())
    {
      return null;
    }
    else
    {
      return resultsList.get(resultsIndex);
    }
  }

  public <T> List<T> getColumnValue(int columnIndex, Class<T> clazz)
  {
    if (columnIndex < 0 || columnIndex >= getColumnCount())
    {
      return null;
    }
    else
    {
      List<T> list = new ArrayList<T>(getResultsCount());
      for (Object[] results : resultsList)
      {
        list.add((T) results[columnIndex]);
      }

      return list;
    }
  }

  public <T> T getValue(int resultsIndex, int columnIndex, Class<T> clazz)
  {
    if (columnIndex < 0 || columnIndex >= getColumnCount() || resultsIndex < 0 || resultsIndex >= getResultsCount())
    {
      return null;
    }
    else
    {
      return (T) resultsList.get(resultsIndex)[columnIndex];
    }
  }

  public int getResultsCount()
  {
    return resultsList.size();
  }

  public int getColumnCount()
  {
    return columnCount;
  }

  public boolean isResultsRmpty()
  {
    return resultsList.isEmpty();
  }
}
